package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game70Ba;

import org.json.JSONObject;

import androidx.appcompat.app.AppCompatActivity;

public class balanceActivity70 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ListView lv_gam70_ba;
    private RequestQueue queue;
    private ProgressDialog show;
    private Game70Ba json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance70);
        queue = Volley.newRequestQueue(this);
        initView();
        initData();
    }

    private void initData() {
        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "ETCbalance",  new Response.Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject jsonObject) {

                hide();
                json = new Gson().fromJson(jsonObject.toString(), Game70Ba.class);
                lv_gam70_ba.setAdapter(new AdapterLive());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(balanceActivity70.this, "网络异常！", Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(request);

    }

    private void show(){
        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();
    }
    private void hide(){
        show.dismiss();
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        lv_gam70_ba = findViewById(R.id.lv_gam70_ba);


        tv_content_title.setText("ETC余额");
        tv_left_text.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);
        iv_rigth_image.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.left);
    }

    class AdapterLive extends BaseAdapter {

        @Override
        public int getCount() {
            return json.getData().size();
        }

        @Override
        public Object getItem(int position) {
            return json.getData().get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(balanceActivity70.this, R.layout.lv_balance_70, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Game70Ba.DataBean bean = json.getData().get(position);
            holder.tv_ba_id.setText(bean.getId());
            holder.tv_ba_price.setText("余额：" + bean.getBalance());
            holder.tv_ba_number.setText(bean.getCarNumbser() + "\n" + bean.getCar());

            final ViewHolder finalHolder = holder;
            ImageRequest request = new ImageRequest(bean.getImage(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    finalHolder.iv_ba_image.setImageBitmap(bitmap);
                }
            },0,0, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888,null);
            queue.add(request);

            return convertView;
        }

        class ViewHolder {
            public View rootView;
            public TextView tv_ba_id;
            public ImageView iv_ba_image;
            public TextView tv_ba_number;
            public TextView tv_ba_price;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.tv_ba_id = (TextView) rootView.findViewById(R.id.tv_ba_id);
                this.iv_ba_image = (ImageView) rootView.findViewById(R.id.iv_ba_image);
                this.tv_ba_number = (TextView) rootView.findViewById(R.id.tv_ba_number);
                this.tv_ba_price = (TextView) rootView.findViewById(R.id.tv_ba_price);
            }

        }
    }

}
